package laborum2011.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import laborum2011.dao.*;
import laborum2011.excepcion.DAOExcepcion;
import laborum2011.modelo.*;

/**
 * Servlet implementation class EmpresaServlet
 */
@WebServlet("/EmpresaServlet")
public class EmpresaServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public EmpresaServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.processRequest(request, response);

	}

	private String validar(String razonSocial, String ruc, String rubro,
			String descripcion, String direccion, String telefono, String web,
			String cargo, String correo) {
		String val = "";
		
		if(razonSocial == ""){
			val = "Ingresar Razon Social";
			return val;
		}else if(ruc == ""){
			val = "Ingresar Ruc";
			return val;
		}else if(rubro== ""){
			val = "Ingresar rubro";
			return val;
		}else if(descripcion== ""){
			val = "Ingresar descripcion";
			return val;
		}else if(direccion== ""){
			val = "Ingresar direccion";
			return val;
		}else if(telefono== ""){
			val = "Ingresar telefono";
			return val;
		}else if(web== ""){
			val = "Ingresar web";
			return val;
		}else if(cargo== ""){
			val = "Ingresar cargo";
			return val;
		}else if(correo== ""){
			val = "Ingresar correo";
			return val;
		}
		
		return "valido";
	}

	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	
		Contacto co = new Contacto();
		EmpresaDAO voemp = new EmpresaDAO();
		Empresa emp = new Empresa();

		String boton = request.getParameter("tipo");
		HttpSession session = request.getSession();
		
		
		String var = "";
		int idpersona= 0;
		int estado = 1;
		
		
		String razonSocial 	= request.getParameter("razonSocial");
		String ruc 			= request.getParameter("ruc");
		String rubro 		= request.getParameter("rubro");
		String descripcion  = request.getParameter("descripcion");
		String direccion 	= request.getParameter("direccion");
		String telefono     = request.getParameter("telefono");
		String web 			= request.getParameter("web");
		String cargo 		= request.getParameter("cargo");
		String correo 		= request.getParameter("correo");

		var=validar(razonSocial, ruc, rubro, descripcion, direccion, telefono, web, cargo, correo);
	     if (var.equals("valido")){
	    	 session.setAttribute("anunciol", ""); 
	     }else {
	    	 session.setAttribute("anunciol", var);	 
	     }
	     if (boton == "ingresoe" ) {
	    	idpersona = (Integer) session.getAttribute("idpersona");
	    	System.out.println(idpersona);
	    	 co.setId_persona(idpersona);
	    	 co.setCargo(cargo);
	    	 co.setEstado(estado);

	    	 emp.setRazon_social(razonSocial);
	    	 emp.setRUC(ruc);
	    	 emp.setRubro(rubro);
	    	 emp.setDesc_negocio(descripcion);
	    	 emp.setDireccion(direccion);
	    	 emp.setTelefono(telefono);
	    	 emp.setWeb(web);
	    	 emp.setCorreo(correo);
	    	 emp.setEstado(estado);
	    	 try {
				//lid = voco.insertar(co);
				//System.out.println(lid);
				//emp.setId_contacto(lid);
				voemp.insertar(emp);
			} catch (DAOExcepcion e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    	 
	     }
		RequestDispatcher rd = request.getRequestDispatcher("/IngresoEmpresa.jsp");
		rd.forward(request, response);
	}

}
